"use_strict";
const fs = require('fs');
const _ver = 'v1.0.2'
const ext_path = './extensions'

if(!fs.existsSync("./config")){   //创建配置文件目录
    fs.mkdirSync("./config");
};

global.LB = {}; //全局对象

LB.HELPER = {};     //工具对象
LB.CONFIG = {};     //配置信息对象
LB.EXTENSION = {};  //扩展功能对象
LB.MAIN = {         //核心功能对象
    GROUP:[],
    WEBSOCKET:[]
};

var logo = '\n ╦   ┬ ┌┬┐ ┌─┐ ╔╗  ┌─┐ ┌┬┐\n ║   │  │  ├┤  ╠╩╗ │ │  │ \n ╩═╝ ┴  ┴  └─┘ ╚═╝ └─┘  ┴ \n\n # Powered by OICQ\n # Version '+_ver+'\n # ©2022 route246 All rights reserved.\n';
console.log(logo);

require('./core/helper/logger');             //控制台输出模块
require('./core/helper/oicq');               //QQ登录模块
var mode = LB.CONFIG.global.ws_mode
setTimeout(()=>{
    require('./core/helper/crypto');         //加密模块
    switch(mode){
        case 0:
            require('./core/ws_send_mode0');     //WebSocket消息处理模块（发送侧，服务器模式）
        break;

        case 1:
            require('./core/ws_send_mode1');     //WebSocket消息处理模块（发送侧,客户端模式）
        break;
    }
    require('./core/helper/ws_encrypt');     //WebSocket消息加密模块
    require('./core/helper/regex');          //正则表达式处理模块
    require('./core/helper/apd_helper');     //玩家数据处理模块
    require('./core/helper/sys_info');       //系统信息查询模块
    require('./core/helper/msg_format');     //文本格式化模块
    require('./core/helper/http(s)');        //HTTP(S)异步请求模块
    require('./core/group_msg_proc');   //群消息处理模块
    require('./core/ws_msg_recv');      //WebSocket消息处理模块（接收侧）

    fs.readdir(ext_path, function(err, files){
        //err 为错误 , files 文件名列表包含文件夹与文件
        if(err){
            console.log('error:\n' + err);
            return;
        }
        files.forEach(function(file){
            fs.stat(ext_path + '/' + file, function(err, stat){
                if(err){console.log(err); return;}
                if(stat.isDirectory()){                 
                    // 如果是文件夹遍历
                    explorer(ext_path + '/' + file);
                }else{
                    // 读出所有的文件
                    require(ext_path + '/' + file);
                    LB.log.ok('扩展' + file + '已加载！');
                }               
            });
        });
        });
},2000);

process.stdin.on("data", (input)=>{
    let i = input.toString().trim();
    switch(i){
        default:
            LB.log.error("[LiteBot CLI] 未知的指令："+i+"。请检查输入是否正确。");
        break;

        case "stop\nend\nexit": case "stop":
            LB.log.info("正在关闭LiteBot，请稍后...\n");
            setTimeout(()=>{process.exit(0)},1000);
        break;

        case "version":
            LB.log.info("版本信息：LiteBot "+_ver);
        break;
    };
});